package com.example.note;

import com.example.note.adapter.MyAdapter;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SearchView;

public class MainActivity extends Activity implements OnQueryTextListener {
	ImageButton btnAdd;
	Button btnRefresh;
	ActionBar actionbar;
	DatabaseHanler db;
	MyAdapter adapter;
	ListView lstView;
	SearchView mSearchView;
	EditText inputSearch;

	public void getControl() {

		btnAdd = (ImageButton) findViewById(R.id.btnAdd);
	}

	public void setEvent() {
		btnAdd.setOnClickListener(new MyProcess());
		lstView = (ListView) findViewById(R.id.lstView);
	}

	public void InsertNote() {
		Intent i = new Intent(MainActivity.this, Insert_note.class);
		startActivity(i);
	}

	private class MyProcess implements OnClickListener {

		// create tables
		@Override
		public void onClick(View v) {

			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnAdd:
				InsertNote();
				break;
			}
		}
	}

	public void getData() {
		db = new DatabaseHanler(this);
		adapter = new MyAdapter(this, db.getAllNote());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
		getViewandEventListView();
	}

	public void getViewandEventListView() {

		lstView.setAdapter(adapter);
		lstView.setTextFilterEnabled(true);
		lstView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Notes note = db.getAllNote().get(position);
				// String item = note.getTitle();

				Intent ii = new Intent(MainActivity.this, Insert_note.class);
				ii.putExtra("idNote", String.valueOf(note.getID()));
				ii.putExtra("title", note.getTitle());
				ii.putExtra("content", note.getContent());
				startActivity(ii);
			}

		});
		lstView.setTextFilterEnabled(true);
	}

	// tinh chinh search view
	private void setupSearchView() {
		mSearchView.setIconifiedByDefault(false);
		mSearchView.setOnQueryTextListener(this);
		mSearchView.setSubmitButtonEnabled(true);
		mSearchView.setQueryHint("Search Here");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getControl();
		setEvent();
		getData();
		getViewandEventListView();

		// setupSearchView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		MenuItem itemSearch = menu.findItem(R.id.action_search);
		mSearchView = (SearchView) itemSearch.getActionView();
		setupSearchView();
		return true;

	}

	@Override
	public boolean onQueryTextSubmit(String arg0) {

		return false;
	}

	@Override
	public boolean onQueryTextChange(String arg0) {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(arg0)) {
			lstView.clearTextFilter();
		} else {
			lstView.setFilterText(arg0.toString());
		}
		return true;
	}
}
